package com.yidu.hbats.action.TrainingPlan;

import com.yidu.hbats.action.vo.AtsPlanPhasevo;
import com.yidu.hbats.model.*;
import com.yidu.hbats.service.TrainingPlan.AtsEmpAthleteSchemeService;
import com.yidu.hbats.service.TrainingPlan.AtsPlanItemService;
import com.yidu.hbats.service.atsBase.AtsBaseSportService;
import com.yidu.hbats.service.TrainingPlan.AtsPlanPhaseService;
import com.yidu.hbats.service.emp.AtsEmpCoachService;
import com.yidu.utils.AtsPlanItemTree;
import com.yidu.utils.FileTools;
import com.yidu.utils.Message;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 韩红
 * @类的描述——
 * @date 2019/10/30下午 06:25
 */
@Controller
@RequestMapping("atsPlanPhaseController")
public class AtsPlanPhaseController {

    @Resource
    AtsPlanPhaseService atsPlanPhaseService;
    //体育训练表的注入
    @Resource
    AtsBaseSportService atsBaseSportService;
    //教练表的注入
    @Resource
    AtsEmpCoachService atsEmpCoachService;
    //文件上传管理工具
    @Resource
    FileTools fileTools;
    //指标表
    @Resource
    AtsPlanItemService atsPlanItemService;
    @Resource
    AtsEmpAthleteSchemeService atsEmpAthleteSchemeService;

    /**
     * 根据Id查询
     * @param atsPlanItem
     * @param atsPlanPhase
     * @return
     */
    @RequestMapping("phaseById")
    @ResponseBody
    public Map<String, Object> findById(AtsPlanItem atsPlanItem, AtsPlanPhase atsPlanPhase) {
        Map<String, Object> map = atsPlanItemService.phaseSelectPid(atsPlanItem, atsPlanPhase);
        return map;
    }

    /**
     * 图表展示
     * @param atsPlanItem
     * @return
     */
    @RequestMapping("phaseEchar")
    @ResponseBody
    public List<AtsPlanItemTree> echar(AtsPlanItem atsPlanItem) {
        List<AtsPlanItemTree>  treeList = atsPlanItemService.phaseEchar(atsPlanItem);
        return treeList;

    }

    /**
     * 逻辑删除
     * @param atsPlanPhase
     * @return
     */
    @RequestMapping("updateIsva")
    @ResponseBody
    public Message updateIsva(AtsPlanPhase atsPlanPhase) {
        //调用逻辑删除
        int rows = atsPlanPhaseService.updateIsva(atsPlanPhase);
        //删除计划同时删除下面指标
        AtsPlanItem atsPlanItem=new AtsPlanItem();
        //将计划设置为不可用
        atsPlanItem.setIsva(0);
        //取得上级指标
        atsPlanItem.setPid(atsPlanPhase.getPhaseId());
        //删除训练运动员里对应的运动员
        AtsEmpAthleteScheme atsEmpAthleteScheme=new AtsEmpAthleteScheme();
        //将训练运动员设置为不可用
        atsEmpAthleteScheme.setIsva(0);
        //取得上级指标
        atsEmpAthleteScheme.setPid(atsPlanPhase.getPhaseId());
        Message mes = new Message();
        //判断是否删除成功
        if (rows >= 1) {
            //调用修改三方法
            int rowso=atsEmpAthleteSchemeService.updatePidThree(atsEmpAthleteScheme);
            int rowst=atsPlanItemService.updateOrdeletefour(atsPlanItem);
            mes.setNum(1);
            mes.setMsg("修改成功");
        } else {
            mes.setNum(0);
            mes.setMsg("修改失败");
        }


        return mes;
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @RequestMapping("delAll")
    @ResponseBody
    public Message delAll(String id) {
        //调用删除方法
        int rows = atsPlanPhaseService.delAll(id);
        //新建信息类
        Message mes = new Message();
        if (rows >= 1) {
            mes.setNum(1);
            mes.setMsg("修改成功");
        } else {
            mes.setNum(0);
            mes.setMsg("修改失败");
        }


        return mes;
    }

    /**
     * 查询所有
     * @param request
     * @param atsPlanPhase
     * @param pageNum
     * @param pageSize
     * @return
     */
    @RequestMapping("selectAll")
    @ResponseBody
    public Map<String, Object> selectAll(HttpServletRequest request,AtsPlanPhase atsPlanPhase, @RequestParam("page") Integer pageNum, @RequestParam("limit") Integer pageSize) {
        //查询行数
        int count = atsPlanPhaseService.count(request,atsPlanPhase);
        List<AtsPlanPhase> list = atsPlanPhaseService.selectAll(request,atsPlanPhase,pageNum,pageSize);
        Map<String, Object> map = new HashMap<>();
        map.put("data", list);
        map.put("count", count);
        map.put("code", 0);
        map.put("msg", "");//消息
        return map;
    }
    /**
     * 根据主键查询
     * @param phaseId
     * @return
     */
    @ResponseBody
    @RequestMapping("findById")
    public AtsPlanPhase findById(Integer phaseId){
        AtsPlanPhase atsPlanYearMore=atsPlanPhaseService.selectByPrimaryKey(phaseId);
        return atsPlanYearMore;
    }

    /**
     * 增加或者修改
     * @param atsPlanYearMorevo
     * @param session
     * @return
     */
    @ResponseBody
    @RequestMapping("addOrupdate")
    public Message addOrupdate(AtsPlanPhasevo atsPlanYearMorevo, HttpSession session){
        //设置排序
        atsPlanYearMorevo.setRemark("3");
        //设置是否可用
        atsPlanYearMorevo.setIsva(1);
        Message message=new Message();
        //根据下拉框的教练id查出教练名
        AtsEmpCoach atsEmpCoach=atsEmpCoachService.findById(atsPlanYearMorevo.getCoachId());
        //传入体育项目名
        atsPlanYearMorevo.setItemName(atsPlanYearMorevo.getSportName());
        //传入教练名
        atsPlanYearMorevo.setCoachName(atsEmpCoach.getCoachName());
        //判断附件是否上传成功
        if(atsPlanYearMorevo.getFileone().getOriginalFilename()!=null &&!"".equals(atsPlanYearMorevo.getFileone().getOriginalFilename())){
            try {
                //调用文件工具类的保存方法
                String file1=fileTools.saveFile("TrainingPlan",atsPlanYearMorevo.getFileone());
                //设置附件1
                atsPlanYearMorevo.setFile1(file1);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        if(atsPlanYearMorevo.getFiletwo().getOriginalFilename()!=null &&!"".equals(atsPlanYearMorevo.getFiletwo().getOriginalFilename())){
            try {
                String file2 = fileTools.saveFile("TrainingPlan",atsPlanYearMorevo.getFiletwo());
                atsPlanYearMorevo.setFile2(file2);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        if(atsPlanYearMorevo.getFilethree().getOriginalFilename()!=null &&!"".equals(atsPlanYearMorevo.getFilethree().getOriginalFilename())){
            try {
                String file3=fileTools.saveFile("TrainingPlan",atsPlanYearMorevo.getFilethree());
                atsPlanYearMorevo.setFile3(file3);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        //ManagerModel managerModel= (ManagerModel) session.getAttribute("manager");
        //取到当前登录人
        //managerModel.getManUser();
        //atsPlanYearMorevo.setCreater("admin");
        //给指标传创建人
        //atsPlanYearMorevo.setCreateName("");
        if(atsPlanYearMorevo.getPhaseId()!=null &&!"".equals(atsPlanYearMorevo.getPhaseId())){
            atsPlanPhaseService.updateByPrimaryKeySelective(atsPlanYearMorevo);
        }else{
            atsPlanPhaseService.insertSelective(atsPlanYearMorevo);
        }
        //传成功
        message.setNum(1);
        message.setMsg("操作成功!");
        return message;
    }
    @ResponseBody
    @RequestMapping("findAll")
    public List<AtsPlanPhase> findAll(AtsPlanPhase atsPlanPhase){
        List<AtsPlanPhase> list=atsPlanPhaseService.selectAll(atsPlanPhase);
        return list;
    }
    @ResponseBody
    @RequestMapping("findAllTwo")
    public List<AtsPlanPhase> findAll(String creater){
        List<AtsPlanPhase> list=atsPlanPhaseService.selectAllTwo(creater);
        return list;
    }
    /**
     * 查询当前最大的id
     * @return
     */
    @ResponseBody
    @RequestMapping("findMaxPid")
    public AtsPlanPhase findMaxPid(int coachId){
        AtsPlanPhase atsPlanWeek=atsPlanPhaseService.findMaxPid(coachId);
        return atsPlanWeek;
    }
    @ResponseBody
    @RequestMapping("findPlanItemYearMore")
    public List<AtsPlanItem> findPlanItemYearMore(Integer pid){
        List<AtsPlanItem> list=atsPlanItemService.findPlanItemYearMorefour(pid);
        return list;
    }
    @ResponseBody
    @RequestMapping("findPlanItemYearMoreone")
    public List<AtsPlanItem> findPlanItemYearMoreone(Integer pid){
        List<AtsPlanItem> list=atsPlanItemService.findPlanItemYearMorefour(pid);
        return list;
    }
    @ResponseBody
    @RequestMapping("deleteZhibiao")
    public Message deleteZhibiao(Integer itemId){
        AtsPlanItem atsPlanItem=new AtsPlanItem();
        atsPlanItem.setIsva(0);
        atsPlanItem.setItemId(itemId);
        Message message=new Message();
        int rows=atsPlanItemService.updateByPrimaryKeySelectivefour(atsPlanItem);
        if(rows>0){
            message.setMsg("删除成功!");
            message.setNum(1);
        }else{
            message.setMsg("删除失败!");
            message.setNum(0);
        }
        return message;
    }
    @ResponseBody
    @RequestMapping("findDivone")
    public List<AtsPlanItem> findDivone(){
        List<AtsPlanItem> list=atsPlanItemService.findDivfour();
        return list;
    }
}
